<template>
    <section>
        <!--工具条-->

        <div class="section-content clearfix">
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">
                    <el-form-item style="float: left;">
                        <el-input v-model="filters.name" placeholder="菜单名" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>
                    <el-form-item class="tool-bt">
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button :type="buttonColor" @click="handleAdd">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <el-table :data="query" :cell-style="linecolor" row-key="id" :header-row-style="{backgroundColor: titleColor}" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"  @selection-change="selsChange" style="width: 100%;">
                <el-table-column prop="name" label="菜单名称"></el-table-column>
                <el-table-column prop="englishName" label="英文名字"></el-table-column>
                <el-table-column label="icon">
                    <template scope="scope">
                       <i style="font-size:25px;" :class="scope.row.icon"></i>
                    </template>
                </el-table-column>
                <el-table-column prop="path" label="地址"></el-table-column>
                <el-table-column label="优先级"></el-table-column>
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                        <i @click="handleEdit(scope.$index, scope.row)" title="编辑" icon="el-icon-edit" circle class="pb-icon icon-edit"></i>
                        <i @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete" circle title="删除" class="pb-icon icon-delete"></i>
                    </template>
                </el-table-column>
            </el-table> 
        </div>
        <!--编辑界面-->
        <el-dialog title="编辑" v-show="editFormVisible" v-model="editFormVisible" :custom-class="dialogTitleColor" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="菜单中文名" prop="name">
                        <el-input v-model="editForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单英文名" prop="name">
                        <el-input v-model="editForm.englishName" auto-complete="off"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="父菜单ID">
                            <el-radio-group v-model="addForm.sex">
                                <el-radio class="radio" :label="1">男</el-radio>
                                <el-radio class="radio" :label="0">女</el-radio>
                            </el-radio-group>
                        </el-form-item> -->
                    <el-form-item label="父菜单">
                        <!-- <el-input-number v-model="editForm.parentId" :min="0"></el-input-number> -->
                        <el-select v-model="editForm.parentId" placeholder="请选择父级">
                            <el-option label='主菜单' value='0'></el-option>
                            <el-option
                            v-for="item in query"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="优先级">
                        <el-input-number v-model="editForm.priority" :min="0"></el-input-number>
                    </el-form-item>
                    <el-form-item label="是否需要图标">
                        <el-radio v-model="editForm.needIcon" label="0">是</el-radio>
                        <el-radio v-model="editForm.needIcon" label="1">否</el-radio>
                    </el-form-item>
                    <el-form-item label="是否隐藏">
                        <el-radio v-model="editForm.status" :label="0">是</el-radio>
                        <el-radio v-model="editForm.status" :label="1">否</el-radio>
                    </el-form-item>
                    <el-form-item label="icon" class="menuicon" v-if="editForm.needIcon==0">
                        <el-popover trigger="click" placement="bottom-start" width="460">
                            <div class="icon-list">
                                <div v-for="item in iconList" @click="editIcon('pb-icon '+item.font_class)" style="position:relative;margin-bottom:5px;display:flex;">
                                    <i :class="'pb-icon '+item.font_class"  style="font-size:25px;"/>
                                     <span>{{ item.font_class }}</span>
                                </div>
                                <div v-for="(item, index) in menuList.glyphs" :key="index" @click="editIcon('iconfont icon-'+item.font_class)" style="position:relative;margin-bottom:5px;display:flex;">
                                    <i :class="'iconfont icon-'+item.font_class"  style="font-size:25px;"/>
                                    <span style="padding-left:5px;">{{ 'icon-'+item.font_class }}</span>
                                </div>
                            </div>
                            <el-input slot="reference" v-model="editForm.icon" :min="0" readonly>
                                <i slot="prefix" :class="editForm.icon" style="font-size:25px;"></i>
                            </el-input>
                        </el-popover>
                    </el-form-item>
                    <el-form-item label="path" prop="path"  class="grade">
                        <el-input type="text" v-model="editForm.path"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:right;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="editSubmit" :loading="editLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" :custom-class="dialogTitleColor" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="菜单中文名" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off" class="grade"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单英文名" prop="name">
                        <el-input v-model="addForm.englishName" auto-complete="off" class="grade"></el-input>
                    </el-form-item>

                    <!-- <el-form-item label="父菜单ID">
					<el-radio-group v-model="addForm.sex">
						<el-radio class="radio" :label="1">男</el-radio>
						<el-radio class="radio" :label="0">女</el-radio>
					</el-radio-group>
				</el-form-item> -->
                    <el-form-item label="父菜单">
                        <!-- <el-input-number v-model="addForm.parentId" :min="0"></el-input-number> -->
                        <el-select v-model="addForm.parentId" placeholder="请选择父级">
                            <el-option label='主菜单' value='0'></el-option>
                            <el-option
                            v-for="item in query"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="优先级">
                        <el-input-number v-model="addForm.priority" :min="0"></el-input-number>
                    </el-form-item>
                    <el-form-item label="icon" class="menuicon">
                        <!-- <el-input v-model="addForm.icon" :min="0"></el-input> -->
                        <el-popover trigger="click" placement="bottom-start" width="460">
                            <div class="icon-list">
                                <div v-for="item in iconList" @click="addIcon('pb-icon '+item.font_class)" style="position:relative;margin-bottom:5px;display:flex;">
                                    <i :class="'pb-icon '+item.font_class"  style="font-size:25px;"/>
                                    <span>{{ item.font_class }}</span>
                                </div>
                                <div v-for="(item, index) in menuList.glyphs" :key="index" @click="addIcon('iconfont icon-'+item.font_class)" style="position:relative;margin-bottom:5px;display:flex;">
                                    <i :class="'iconfont icon-'+item.font_class"  style="font-size:25px;"/>
                                    <span style="padding-left:5px;">{{ 'icon-'+item.font_class }}</span>
                                </div>
                            </div>
                            <el-input slot="reference" class="iconInput" v-model="addForm.icon" :min="0" readonly>
                                <i slot="prefix" :class="addForm.icon" style="font-size:25px;"></i>
                            </el-input>
                        </el-popover>
                    </el-form-item>
                    <el-form-item label="path" prop="path">
                        <el-input type="text" v-model="addForm.path" class="grade"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: center;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    // import util from './../common/js/util'
    //import NProgress from 'nprogress'
    import {
        query,
        querypost,
        queryput,
        querydelete,
        queryget
    } from './../api/api';
    import menujson from '../assets/menuIcon/iconfont.json'
    export default {
        data() {
            return {
                filters: {
                    name: '',
                },
                query: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列

                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请输入模板名菜单名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }]
                },
                //编辑界面数据
                editForm: {
                    id: '',
                    type: '',
                    name: '',
                    englishName: '',
                    parentId: '',
                    path: '',
                    icon: '',
                    status: '',
                    priority: '',
                    createTime: '',
                    needIcon: 1,
                    status:''
                },

                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入模板名菜单名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                    path: [{
                            required: true,
                            message: '请输入path',
                            trigger: 'blur'
                        }

                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    englishName: '',
                    parentId: '',
                    path: '',
                    icon:''
                },
                iconList:[
                    {font_class:'icon-home1'},
                    {font_class:'icon-home2'},
                    {font_class:'icon-home3'},
                    {font_class:'icon-home4'},
                    {font_class:'icon-home5'},
                    {font_class:'icon-home6'},
                    {font_class:'icon-home7'},
                    {font_class:'icon-home8'},
                    {font_class:'icon-home9'},
                    {font_class:'icon-home10'},
                    {font_class:'icon-home11'},
                    {font_class:'icon-home12'},
                    {font_class:'icon-home13'},
                    {font_class:'icon-home14'},
                    {font_class:'icon-home15'},
                    {font_class:'icon-home16'},
                    {font_class:'icon-home17'},
                    {font_class:'icon-home18'},
                    ],
                menuList:[]
            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods: {
            editIcon(name){
                console.log(name)
                this.editForm.icon = name
                document.body.click()
            },
            addIcon(name){
                console.log(name)
                this.addForm.icon = name
                document.body.click()
            },
            linecolor({row, column, rowIndex, columnIndex}){
                let lineStyle
                if(columnIndex === 2){
                    lineStyle = "cursor: unset"
                }
                lineStyle = "background:#cfe0e6"
                return lineStyle
            },
            //性别显示转换
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            //获取菜单列表
            getUsers() {
                let para = {
                    pageNo: this.page,
                    // pageSize: 10,
                    pageSize: 999,
                    name: this.filters.name,
                };
                this.listLoading = true;
                //NProgress.start();
                query(para).then((res) => {
                    this.total = res.data.totalCount;
                    this.query = this.handleTree(res.data.list)
                    // this.query = res.data.list;
                    this.listLoading = false;
                    //NProgress.done();
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        id: row.id
                    };
                    querydelete(para).then((res) => {
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();

                        } else {
                            this.$message({
                                message: res.message,
                                type: 'warning'
                            });
                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row,{needIcon:"0"});
                console.log(this.editForm)
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    englishName: '',
                    parentId: '',
                    path: '',
                    icon:''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            if(this.editForm.needIcon == '1'){
                                this.editForm.icon = ''
                            }
                            let para = Object.assign({}, this.editForm);
                            para.children=[]
                            queryput(para).then((res) => {
                                if (res.code == 0) {
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                } else {
                                    this.$message({
                                        message: res.message,
                                        type: 'warning'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(
                                new Date(para.birth), 'yyyy-MM-dd');
                            querypost(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        ids: ids
                    };
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            },
        
        handleTree(data, id, parentId, children, rootId) {//数据转换成树状图
           var id = id || 'id'
           var parentId = parentId || 'parentId'
           var children = children || 'children'
           var rootId = rootId || Math.min.apply(Math, data.map(item => { return item[parentId] })) || 0
            //对源数据深度克隆
            const cloneData = JSON.parse(JSON.stringify(data))
            //循环所有项
            const treeData = cloneData.filter(father => {
                
                let branchArr = cloneData.filter(child => {
                    //返回每一项的子级数组
                    return father[id] === child[parentId]
                });
                branchArr.length > 0 ? father.children = branchArr : '';
                //返回第一层
                return father[parentId] == rootId;
            });
            return treeData != '' ? treeData : data;
        }
        },
        mounted() {
            this.getUsers();
            // Object.assign(menujson.glyphs,this.iconList)
            this.menuList = menujson
            // console.log(meet)
        }
    }
</script>

<style>
.menuicon .el-input input{
    /* background: rgb(177,223,237); */
    background: rgb(151 163 167);
    /* background: #F5F7FA; */
    padding-left: 40px;
}
.icon-list {
    height: 200px;
    overflow-y: scroll;
    /* background: #F5F7FA; */
    /* background: rgb(177,223,237); */
    background: rgb(151 163 167);
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
}
.icon-list>div {
    height: 30px;
    line-height: 30px;
    margin-bottom: -5px;
    cursor: pointer;
    width: 26%;
    float: left;
    white-space: nowrap;
    overflow: hidden;
}
.icon-list>span{
    display: inline-block;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
}
.pb-icon{
   vertical-align: middle;
}
/* .iconInput>span{
    display: flex;
    align-items: center;
} */
</style>